package com.activiti.z_six.controller;

import com.activiti.z_six.controller.controllerParams.UserParams;
import com.activiti.z_six.entity.orgmanagement.PositionEntity;
import com.activiti.z_six.entity.orgmanagement.RolesEntity;
import com.activiti.z_six.entity.orgmanagement.UserEntity;
import com.activiti.z_six.service.OrgManagementService;
import com.activiti.z_six.util.AjaxResponse;
import com.activiti.z_six.util.SystemConfig;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@RestController
public class orgManagementController {
    @Autowired
    private OrgManagementService orgManagementService;

    /***
     * 获取公司列表
     * @return
     */
    @GetMapping("/orgm/getCompanyList")
    public AjaxResponse getCompanyList(){
        return orgManagementService.getCompanyList();
    }

    /**
     * 增加公司
     * @param param
     * @return
     */
    @PostMapping(value = "/orgm/addCompany")
    public AjaxResponse addCompany(@RequestBody JSONObject param){
        String name=param.getString("name");
        Integer parentid=param.getInteger("parentid");
        return orgManagementService.addCompany(name,parentid);
    }

    /**
     * 修改公司信息
     * @param param
     * @return
     */
    @PostMapping(value = "/orgm/updateCompany")
    public AjaxResponse updateCompany(@RequestBody JSONObject param){
        Integer id=param.getInteger("id");
        String name=param.getString("name");
        Integer parentid=param.getInteger("parentid");
        return orgManagementService.updateCompany(id,name,parentid);
    }

    /**
     * 删除公司信息
     * @param param
     * @return
     */
    @PostMapping(value = "/orgm/deleteCompany")
    public AjaxResponse deleteCompany(@RequestBody JSONObject param){
        Integer id=param.getInteger("id");
        String name=param.getString("name");
        Integer parentid=param.getInteger("parentid");
        return orgManagementService.deleteCompany(id,name,parentid);
    }

    /**
     * 获取部门列表
     * @return
     */
    @GetMapping("/orgm/getDepartmentList")
    public AjaxResponse getDepartmentList(){
        return orgManagementService.getDepartmentList();
    }

    /**
     * 获取公司下的部门
     * @param companyId
     * @return
     */
    @GetMapping("/orgm/getDeptByCompany")
    public AjaxResponse getDeptByCompany(@RequestParam("companyId") Integer companyId){
        return orgManagementService.getDeptByCompany(companyId);
    }
    /**
     * 增加部门
     * @param param
     * @return
     */
    @PostMapping(value = "/orgm/addDepartment")
    public AjaxResponse addDepartment(@RequestBody JSONObject param){
        String name=param.getString("name");
        Integer parentid=param.getInteger("parentid");
        String manager=param.getString("manager");
        String leader=param.getString("leader");
        Integer companyid=param.getInteger("companyid");
        return orgManagementService.addDepartment(name,parentid,manager,leader,companyid);
    }

    /**
     * 修改部门
     * @param param
     * @return
     */
    @PostMapping(value = "/orgm/updateDepartment")
    public AjaxResponse updateDepartment(@RequestBody JSONObject param){
        Integer id=param.getInteger("id");
        String name=param.getString("name");
        Integer parentid=param.getInteger("parentid");
        String manager=param.getString("manager");
        String leader=param.getString("leader");
        Integer companyid=param.getInteger("companyid");
        return orgManagementService.updateDepartment(id,name,parentid,manager,leader,companyid);
    }

    /**
     * 删除部门
     * @param param
     * @return
     */
    @PostMapping(value = "/orgm/deleteDepartment")
    public AjaxResponse deleteDepartment(@RequestBody JSONObject param){
        Integer id=param.getInteger("id");
        return orgManagementService.deleteDepartment(id);
    }

    /**
     * 设置部门负责人
     * @param param
     * @return
     */
    @PostMapping(value = "/orgm/setDepartmentManager")
    public AjaxResponse setDepartmentManager(@RequestBody JSONObject param){
        Integer id=param.getInteger("id");
        String manager=param.getString("manager");
        return orgManagementService.setDepartmentManager(id,manager);
    }

    /**
     * 设置部门分管领导
     * @param param
     * @return
     */
    @PostMapping(value = "/orgm/setDepartmentLeader")
    public AjaxResponse setDepartmentLeader(@RequestBody JSONObject param){
        Integer id=param.getInteger("id");
        String leader=param.getString("leader");
        return orgManagementService.setDepartmentLeader(id,leader);
    }

    /**
     * 设置所属公司
     * @param param
     * @return
     */
    @PostMapping(value = "/orgm/updateDeptCompany")
    public AjaxResponse updateDeptCompany(@RequestBody JSONObject param){
        Integer id=param.getInteger("id");
        Integer companyid=param.getInteger("companyid");
        return orgManagementService.updateDeptCompany(id,companyid);
    }

    /**
     * 获取组织结构数据
     * @return
     */
    @GetMapping("/orgm/getOrgData")
    public AjaxResponse getOrgData(){
        return orgManagementService.getOrgData();
    }

    /**
     * 获取岗位列表
     * @return
     */
    @GetMapping("/orgm/positionList")
    public AjaxResponse positionList(){
        try {
            List<PositionEntity> positionEntities = orgManagementService.positionList();
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), positionEntities);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.toString());
        }
    }

    /**
     * 获取岗位列表--分页
     * @param response
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/orgm/positionListPage")
    public AjaxResponse positionListPage(HttpServletResponse response,
                                         @RequestParam("page") Integer page,
                                         @RequestParam("pagesize") Integer pagesize){
        try{
            HashMap<String,Object> hashMap=orgManagementService.positionListPage(page,pagesize);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), hashMap);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.toString());
        }
    }

    /**
     * 获取用户的岗位
     * @param response
     * @param username
     * @return
     */
    @GetMapping("/orgm/positionListByUser")
    public AjaxResponse positionListByUser(HttpServletResponse response,
                                           @RequestParam("username") String username){
        try {
            List<PositionEntity> positionEntities = orgManagementService.positionListByUser(username);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), positionEntities);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.toString());
        }
    }

    /**
     * 增加岗位
     * @param param
     * @return
     */
    @PostMapping("/orgm/addPosition")
    public AjaxResponse addPosition(@RequestBody JSONObject param){
        String name=param.getString("name");
        return orgManagementService.addPosition(name);
    }

    /**
     * 修改岗位信息
     * @param param
     * @return
     */
    @PostMapping("/orgm/updatePosition")
    public AjaxResponse updatePosition(@RequestBody JSONObject param){
        String id=param.getString("id");
        String name=param.getString("name");
        return orgManagementService.updatePosition(id,name);
    }

    /**
     * 删除岗位
     * @param param
     * @return
     */
    @PostMapping("/orgm/deletePosition")
    public AjaxResponse deletePosition(@RequestBody JSONObject param){
        String id=param.getString("id");
        return orgManagementService.deletePosition(id);
    }

    /**
     * 删除用户岗位
     * @param response
     * @param username
     * @return
     */
    @GetMapping("/orgm/deletePositionByUser")
    public AjaxResponse deletePositionByUser(HttpServletResponse response,
                                             @RequestParam("username") String username){
        return orgManagementService.deletePositionUser(username);
    }
    /**
     * 获取角色列表
     * @return
     */
    @GetMapping("/orgm/rolesList")
    public AjaxResponse rolesList(){
        try {
            List<RolesEntity> rolesEntities = orgManagementService.rolesList();
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), rolesEntities);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.toString());
        }
    }

    /**
     * 获取角色列表--分页
     * @param response
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/orgm/rolesListPage")
    public AjaxResponse rolesListPage(HttpServletResponse response,
                                         @RequestParam("page") Integer page,
                                         @RequestParam("pagesize") Integer pagesize){
        try{
            HashMap<String,Object> hashMap=orgManagementService.rolesListPage(page,pagesize);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), hashMap);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.toString());
        }
    }

    /**
     * 获取用户的角色
     * @param response
     * @param username
     * @return
     */
    @GetMapping("/orgm/rolesListByUser")
    public AjaxResponse rolesListByUser(HttpServletResponse response,
                                           @RequestParam("username") String username){
        try {
            List<RolesEntity> rolesEntities = orgManagementService.rolesListByUser(username);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), rolesEntities);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.toString());
        }
    }

    /**
     * 增加角色
     * @param param
     * @return
     */
    @PostMapping("/orgm/addRoles")
    public AjaxResponse addRoles(@RequestBody JSONObject param){
        String name=param.getString("name");
        return orgManagementService.addRoles(name);
    }

    /**
     * 修改角色信息
     * @param param
     * @return
     */
    @PostMapping("/orgm/updateRoles")
    public AjaxResponse updateRoles(@RequestBody JSONObject param){
        String id=param.getString("id");
        String name=param.getString("name");
        return orgManagementService.updateRoles(id,name);
    }

    /**
     * 删除角色
     * @param param
     * @return
     */
    @PostMapping("/orgm/deleteRoles")
    public AjaxResponse deleteRoles(@RequestBody JSONObject param){
        String id=param.getString("id");
        return orgManagementService.deleteRoles(id);
    }

    /**
     * 删除用户角色
     * @param response
     * @param username
     * @return
     */
    @GetMapping("/orgm/deleteRoleByUser")
    public AjaxResponse deleteRoleByUser(HttpServletResponse response,
                                             @RequestParam("username") String username){
        return orgManagementService.deleteRoleUser(username);
    }

    /**
     * 获取人员列表
     * @return
     */
    @GetMapping("/orgm/getUserEntities")
    public AjaxResponse getUserEntities(){
        try {
            List<UserEntity> userEntities = orgManagementService.getUserEntities();
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), userEntities);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.toString());
        }
    }

    /**
     * 获取人员列表--分页
     * @param response
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/orgm/getUserList")
    public AjaxResponse getUserList(HttpServletResponse response,
                                        @RequestParam("page") Integer page,
                                        @RequestParam("pagesize") Integer pagesize){
        try {
            HashMap<String,Object> userEntities = orgManagementService.getUserList(page,pagesize);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), userEntities);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.toString());
        }
    }
    @GetMapping("/orgm/getUserEntity")
    public AjaxResponse getUserEntity(HttpServletResponse response,
                                      @RequestParam("username") String username){
        try{
            HashMap<String,Object> hashMap=orgManagementService.getUserEntity(username);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), hashMap);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.toString());
        }
    }
    @GetMapping("/orgm/getUserByDepartId")
    public AjaxResponse getUserByDepartId(HttpServletResponse response,
                                          @RequestParam("departid") Integer departid){
        return orgManagementService.getUserByDepartId(departid);
    }
    @PostMapping(value = "/orgm/addUser")
    public AjaxResponse addUser(@RequestBody UserParams param){
         return orgManagementService.addUser(param);
    }
    @PostMapping(value = "/orgm/updateUser")
    public AjaxResponse updateUser(@RequestBody UserParams param){
        return orgManagementService.updateUser(param);
    }
    @PostMapping(value = "/orgm/updatePwd")
    public AjaxResponse updatePwd(@RequestBody JSONObject param){
        String username=param.getString("username");
        String oldPwd=param.getString("oldpwd");
        String newPwd=param.getString("newpwd");
        return orgManagementService.updatePwd(username,oldPwd,newPwd);
    }
    @PostMapping(value = "/orgm/resetPwd")
    public AjaxResponse resetPwd(@RequestBody JSONObject param){
        String username=param.getString("username");
        return orgManagementService.resetPwd(username);
    }
    @PostMapping(value = "/orgm/deleteUser")
    public AjaxResponse deleteUser(@RequestBody JSONObject param){
        Integer id=param.getInteger("id");
        return orgManagementService.deleteUser(id);
    }
}
